/*
 * Project: yygh-parent
 *
 * File Created at 2023/6/24
 *
 * 自定义时间格式代替DATE标签：2023-06-24 19:12:19:12
 */
package com.atguigu.yygh.order.controller;

import com.atguigu.yygh.common.exception.HospitalException;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.order.service.HospitalSetService;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.atguigu.yygh.vo.order.SignInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @author 用户名
 * @Type HospitalSetController.java
 * @Desc
 * @date 2023/6/24 19:12
 */
//@CrossOrigin
@Api("医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")

public class HospitalSetController {
    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation("获取所有医院设置")
    @GetMapping("findAll")
    public Result findAllHospitalSet() throws HospitalException {


//                if(1==1){
//                throw new HospitalException("不能为空",500);
//                }
//        int i=1/0;
        //调用service的方法
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation("新增医院")
    @PostMapping("saveHospitalSet")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        //设置更新时间
        Date date = new Date();
        hospitalSet.setUpdateTime(date);
        hospitalSet.setStatus(1);
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        boolean flag = hospitalSetService.save(hospitalSet);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @ApiOperation("逻辑删除医院设置")
    @DeleteMapping("{id}")
    public Result removeById(@PathVariable("id") Long id) {
        boolean flag = hospitalSetService.removeById(id);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    //5 根据id获取医院设置
    @ApiOperation("根据id获取医院设置")
    @GetMapping("getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") Long id) {
        System.out.println(id);
        return Result.ok(hospitalSetService.getById(id));


    }

    @ApiOperation("修改医院设置")
    @PostMapping("updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet set) {
        boolean update = hospitalSetService.updateById(set);
        if (update) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    //7 批量删除医院设置
    @ApiOperation("批量删除医院设置")
    @DeleteMapping("batchRemove")
    public Result batchRemoveHospitalSet(@RequestBody List<Integer> list) {
        boolean flag = hospitalSetService.removeByIds(list);
        if (flag) {
            return Result.ok();
        } else {
            return Result.fail();
        }

    }

    @ApiOperation("医院设置锁定和解锁")
    @PutMapping("lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
//        HospitalSet hospitalSet = new HospitalSet();
//        hospitalSet.setId(id);
//        hospitalSet.setStatus(status);
        HospitalSet byId = hospitalSetService.getById(id);
        byId.setStatus(status);
        hospitalSetService.updateById(byId);
        return Result.ok();
    }

    @ApiOperation("发送签名秘钥")
    @PutMapping("lockHospitalSet/{id}")
    public Result lockHospitalSet(@PathVariable("id") Long id) {
        HospitalSet byId = hospitalSetService.getById(id);

//        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }




    @ApiOperation("分页查询医院设置")
    @PostMapping("findPageHospSet/{current}/{limit}")
    public Result findPageHospSet(@PathVariable long current
            , @PathVariable long limit
            , @RequestBody(required = false) HospitalSetQueryVo vo) {
        Page<HospitalSet> page = new Page<>(current, limit);

        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        //得到医院名字
        //得到医院编号
        if (vo != null && !StringUtils.isEmpty(vo.getHosname())) {
            wrapper.like("hosname", vo.getHosname());
        }
        if (vo != null && !StringUtils.isEmpty(vo.getHoscode())) {
            wrapper.like("hoscode", vo.getHoscode());
        }


        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);

        return Result.ok(hospitalSetPage);


    }


   
   /** 
    * @Description 
    * @author jams
    * @version 1.17.0 
    * @date 2023/9/8 18:10
    */
   @ApiOperation("分页查询医院设置")
   @PostMapping("getSignInfoVo/{hostCode}")
   public SignInfoVo getSignInfoVo(@PathVariable String hostCode) {
       SignInfoVo s= hospitalSetService.findSignVo(hostCode);
       return s;


   }



}


/**
 * Revision history
 * -------------------------------------------------------------------------
 * <p>
 * Date Author Note
 * -------------------------------------------------------------------------
 * 2023/6/24 用户名 create
 */ 
